package NonBinaryAttributes;
import java.util.ArrayList;
import java.util.Collections;

import util.FileHandler;

public class GlobalItemSetList {
	private static GlobalItemSetList instance;
	private ArrayList<ItemSet> itemSets;
	private boolean sorted;
	
	private GlobalItemSetList() {
		itemSets = new ArrayList<ItemSet>();
		sorted = false;
	}
	
	public static GlobalItemSetList getInstance() {
		if(instance == null) {
			instance = new GlobalItemSetList();
		}
		return instance;
	}
	
	public void addItemSet(ItemSet is) {
		itemSets.add(is);
		sorted = false;
	}

	public ItemSet getItemSet(ArrayList<Item> ant) {
		ItemSet tmp = new ItemSet(false);
		tmp.setItems(ant);
		for(ItemSet is:itemSets) {
			if(is.equals(tmp)) {
				return is;
			}
		}
		return null;
		
	}

	public void generateRules(float minConf) {
		for(int i = 0; i < itemSets.size(); ++i) {
			ItemSet is = itemSets.get(i);
			if(is.isFrequent()) {
				is.generateAssociationRules(minConf);
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public void printAllMaximalItemSets() {
		if(itemSets.size() == 0) return;
		if(!sorted) {
			Collections.sort(itemSets);
			Collections.reverse(itemSets);
			sorted = true;
		}
			
		//save to file
		String s = "Maximal Itemsets\nMinimum frequency:" + NonBinaryMiner.MINFREQPERCENT +"\n";
		int num = 0;
		for(ItemSet is:itemSets) {
			if(is.isFrequent() && is.isMaximal()) {
				s += is + "\n";
				num++;
			}
		}
		if(num == 0) return;
		FileHandler.getInstance().write(s, "Maximal-F"+NonBinaryMiner.MINFREQPERCENT + ".txt");
		FileHandler.getInstance().addToGlobalFile("Maximal - Frequency:" + NonBinaryMiner.MINFREQPERCENT +
				"  Items:" + num + "\n");
		//print on screen
		/*
		for(ItemSet is:itemSets) {
			if(is.isFrequent() && is.isMaximal()) {
				System.out.println(is);
			}
		}*/		
	}

	@SuppressWarnings("unchecked")
	public void printAllClosedItemSets() {
		if(itemSets.size() == 0) return;
		if(!sorted) {
			Collections.sort(itemSets);
			Collections.reverse(itemSets);
			sorted = true;
		}
		//write to file
		int num = 0;
		String s = "Closed Itemsets\nMinimum frequency:" + NonBinaryMiner.MINFREQPERCENT +"\n";
		for(ItemSet is:itemSets) {
			if(is.isFrequent() && is.isClosed()) {
				s += is + "\n";
				++num;
			}
		}
		if(num == 0) return;
		FileHandler.getInstance().write(s, "Closed-F"+NonBinaryMiner.MINFREQPERCENT + ".txt");
		FileHandler.getInstance().addToGlobalFile("Closed - Frequency:" + NonBinaryMiner.MINFREQPERCENT + "  Items:" + num + "\n");
		//print on screen
		/*
		for(ItemSet is:itemSets) {
			if(is.isFrequent() &&  is.isClosed()) {
				System.out.println(is);
			}
		}*/
	}

	@SuppressWarnings("unchecked")
	public void printAllFrequentItemSets() {
		if(itemSets.size() == 0) return;
		if(!sorted) {
			Collections.sort(itemSets);
			Collections.reverse(itemSets);
			sorted = true;
		}
		//write to file
		String s = "Frequent Itemsets\nMinimum frequency:" + NonBinaryMiner.MINFREQPERCENT + "\n";
		int num = 0;
		for(ItemSet is:itemSets) {
			if(is.isFrequent()) {
				s += is + "\n";
				num++;
			}
		}
		if(num == 0) return;
		FileHandler.getInstance().write(s, "Frequent-F"+NonBinaryMiner.MINFREQPERCENT + ".txt");
		FileHandler.getInstance().addToGlobalFile("Frequent - Frequency:" + NonBinaryMiner.MINFREQPERCENT +
				"  Items:" + num + "\n");
		//print on screen
		/*
		for(ItemSet is:itemSets) {
			if(is.isFrequent()) {
				System.out.println(is);
			}
		}*/
	}

	//removes all data
	public void clear() {
		itemSets = new ArrayList<ItemSet>();
		sorted = false;
	}
}
